package com.biz.primus.ms.depot.service;

import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.model.depot.vo.IncrDepotReqVo;
import com.biz.primus.model.depot.vo.IncrEmpReqVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.depot.dao.po.DepotChangeLog;
import com.biz.primus.ms.depot.dao.po.EmployeeChangeLog;
import com.biz.primus.ms.depot.dao.repository.DepotChangeLogRepository;
import com.biz.primus.ms.depot.dao.repository.EmployeeChangeLogRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * DepotChangeLogService
 *
 * @author JKLiues
 * @date 2017年10月24日
 */
@Service
@Slf4j
public class DepotChangeLogService extends AbstractBaseService {

    private final DepotChangeLogRepository depotChangeLogRepository;
    private final EmployeeChangeLogRepository employeeChangeLogRepository;

    @Autowired
    public DepotChangeLogService(DepotChangeLogRepository depotChangeLogRepository, EmployeeChangeLogRepository employeeChangeLogRepository) {
        this.depotChangeLogRepository = depotChangeLogRepository;
        this.employeeChangeLogRepository = employeeChangeLogRepository;
    }

    /**
     * 写入门店更新日志
     *
     * @param reqVos 门店更新请求
     */
    public void saveDepotLog(List<IncrDepotReqVo> reqVos) {
        log.info("门店更新写日志");
        List<DepotChangeLog> collect = reqVos.stream().map(reqVo -> {
            DepotChangeLog log = new DepotChangeLog();
            log.setId(idService.getNextId());
            log.setDepotCode(reqVo.getDepotCode());
            log.setContent(JsonUtils.obj2Json(reqVo));
            return log;
        }).collect(Collectors.toList());
        depotChangeLogRepository.save(collect);
    }

    /**
     * 写入员工更新日志
     *
     * @param reqVos 门店更新请求
     */
    public void saveEmpLog(List<IncrEmpReqVo> reqVos) {
        log.info("门店员工更新写日志");
        List<EmployeeChangeLog> collect = reqVos.stream().map(reqVo -> {
            EmployeeChangeLog log = new EmployeeChangeLog();
            log.setId(idService.getNextId());
            log.setEmployeeCode(reqVo.getEmployeeCode());
            log.setContent(JsonUtils.obj2Json(reqVo));
            return log;
        }).collect(Collectors.toList());
        employeeChangeLogRepository.save(collect);
    }
}